home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18385 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  65 lines

  1. Path: acs.bu.edu!sseltser
  2. From: sseltser@bu.edu (Stan Seltser)
  3. Newsgroups: comp.lang.c++
  4. Subject: Global Objects Initialization
  5. Date: 20 Apr 1996 05:48:26 GMT
  6. Organization: Boston University
  7. Message-ID: <4l9tra$fcd@news.bu.edu>
  8. NNTP-Posting-Host: acs.bu.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Hello there,
  12.  
  13. here is the question for you fans of C++:
  14. imagine class X and class Y
  15.  
  16. class Y
  17. {
  18. int y;
  19. Y(int) {};
  20. ~Y() {};
  21. };
  22.  
  23. class X {
  24.  
  25. public:
  26.  
  27. Y _y;
  28. static const Y Y1;
  29. static const Y Y2;
  30.  
  31. X(Y y=Y1) { y=_y};
  32. ~X() {};
  33. }
  34.  
  35. const Y X::Y1; // defined in some .cc file
  36. const Y X::Y2; // defined in some .cc file
  37.  
  38. this all looks simple and compiles fine and even
  39. works without any problem
  40.  
  41. as long as definitions of Y1 and Y2 are followed by
  42. actual declaration of class X everything is fine
  43.  
  44. but imagine now that both declaration of Y1 and Y2 as 
  45. well as class X happened in global scope which will cause 
  46. problems since order of initialization is not guaranteed by 
  47. compiler and so potentially upon entering X constrcutor
  48. Y1 may be not initalized 
  49.  
  50. so my question is:
  51.  
  52. how can I guarantee that Y1 is always initialized before
  53. declaration of X type object?
  54.  
  55. Scott Meyers in his wonderful book "effective C++ " has kinda
  56. solution  with  initializer class which in his constrcutor 
  57. will initialize some data members and then objects which depend 
  58. on this data members  are initialized with example from iostream 
  59. library , unfortunetely this is not the case
  60. const static cannot be initialized inside any constrcutor only
  61. globally...
  62.  
  63. anybody has any ideas?
  64.  
  65.